package com.burgeon.backend.SHOPBACKEND.rest;
/**
 * 查询会员 精准营销
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.QueryMarketingData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindVipDataByMarketing extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindVipDataByMarketing(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<String> getData(QueryMarketingData mesage) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(mesage), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        if (!"0".equals(code)) {
            System.err.println(str);
            throw new Exception(str);
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<String> data = new ArrayList<String>();

        for (int i = 0; i < jsonArray.length(); i++) {
            data.add(jsonArray.getJSONArray(i).getString(0));
        }
        System.err.println(data.size());
        return data;
    }

    private MultiValueMap Data(QueryMarketingData mesage) {

        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("CARDNO");

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名
        //********************************************************
        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();
        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        HashMap expr1v1v1 = new HashMap();
        HashMap expr1v1v2 = new HashMap();
        HashMap expr1v2v1 = new HashMap();
        HashMap expr1v2v2 = new HashMap();
        HashMap expr2v1v1 = new HashMap();
        HashMap expr2v1v2 = new HashMap();
        HashMap expr2v2v1 = new HashMap();
        HashMap expr2v2v2 = new HashMap();

        paramss.put("combine", "or");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        expr1.put("combine", "or");
        expr1.put("expr1", expr1v1);
        expr1.put("expr2", expr1v2);
        expr2.put("combine", "or");
        expr2.put("expr1", expr2v1);
        expr2.put("expr2", expr2v2);

        expr1v1.put("combine", "or");
        expr1v1.put("expr1", expr1v1v1);
        expr1v1.put("expr2", expr1v1v2);
        expr1v2.put("combine", "or");
        expr1v2.put("expr1", expr1v2v1);
        expr1v2.put("expr2", expr1v2v2);
        expr2v1.put("combine", "or");
        expr2v1.put("expr1", expr2v1v1);
        expr2v1.put("expr2", expr2v1v2);
        expr2v2.put("combine", "or");
        expr2v2.put("expr1", expr2v2v1);
        expr2v2.put("expr2", expr2v2v2);

        expr1v1v1.put("column", "YC_1LABELDEFINE_ID");//
        expr1v1v1.put("condition",  toString(mesage.getHuiling1()) );
        expr1v1v2.put("column", "YC_2LABELDEFINE_ID");//
        expr1v1v2.put("condition", toString(mesage.getNianling2()));
        expr1v2v1.put("column", "YC_3LABELDEFINE_ID");//
        expr1v2v1.put("condition", toString(mesage.getXiaofeicishu3()));
        expr1v2v2.put("column", "YC_4LABELDEFINE_ID");//
        expr1v2v2.put("condition", toString(mesage.getPingjunkedanjia4()));
        expr2v1v1.put("column", "YC_5LABELDEFINE_ID");//
        expr2v1v1.put("condition",toString(mesage.getChuzhizonge5()));
        expr2v1v2.put("column", "YC_6LABELDEFINE_ID");//
        expr2v1v2.put("condition", toString(mesage.getJifen6()));

        HashMap expr2v2v1v1 = new HashMap();
        HashMap expr2v2v1v2 = new HashMap();
        expr2v2v1v1.put("column", "YC_7LABELDEFINE_ID");//
        expr2v2v1v1.put("condition",  toString(mesage.getZuijinxiaofeijiange7() ));
        expr2v2v1v2.put("column", "YC_8LABELDEFINE_ID");//
        expr2v2v1v2.put("condition", toString(mesage.getLeijigouwujine8()));

        HashMap expr2v2v2v1 = new HashMap();
        HashMap expr2v2v2v2 = new HashMap();
        expr2v2v2v1.put("column", "YC_9LABELDEFINE_ID");//
        expr2v2v2v1.put("condition", toString(mesage.getPingjunzhekou9()));
        expr2v2v2v2.put("column", "YC_10LABELDEFINE_ID");//
        expr2v2v2v2.put("condition", toString(mesage.getChuzhiyue10()));


        expr2v2v1.put("combine", "or");
        expr2v2v1.put("expr1", expr2v2v1v1);
        expr2v2v1.put("expr2", expr2v2v1v2);
        expr2v2v2.put("combine", "or");
        expr2v2v2.put("expr1", expr2v2v2v1);
        expr2v2v2.put("expr2", expr2v2v2v2);


        //********************************************************
        params.put("params", paramss);
        params.put("count", "true");
        params.put("range", "50000");

        return DATA("Query", params, account, password);
    }

    private String toString(String data) {
        if (data==null||"".equals(data)){
            return "\" IN ("+ "'00'"+")\"";
        }
        String[] arr = data.replace("\"", "")
                .replace("'", "")
                .replace("[", "")
                .replace("]", "")
                .replace(" ", "")
                .split(",");

        StringBuilder stringBuffer = new StringBuilder();
        for (String str : arr) {
            stringBuffer.append("'")
                    .append(str)
                    .append("'")
                    .append(",");;
        }

        return "\" IN ("+
                stringBuffer.replace(stringBuffer.length() - 1, stringBuffer.length(), "")
                +")\""
                ;

    }

    public static void main(String[] args) {

        FindVipDataByMarketing g = new FindVipDataByMarketing("8.130.161.41:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            QueryMarketingData data = new QueryMarketingData();
            data.setHuiling1("1211");
            System.err.println(g.getData(data));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
